package com.example.demo.util;

import lombok.Data;

import java.util.Map;

@Data
public class RestBean<T> {
    private int code;
    private boolean success;
    private T data;
    private String msg;

    // 私有构造器
    private RestBean(int code, boolean success, T data, String msg) {
        this.code = code;
        this.success = success;
        this.data = data;
        this.msg = msg;
    }

    // 成功响应（无数据无消息）
    public static <T> RestBean<T> success() {
        return new RestBean<>(200, true, null, null);
    }

    // 成功响应（仅消息）
    public static RestBean<Void> success(String msg) {
        return new RestBean<>(200, true, null, msg);
    }

    // 成功响应（带数据）
    public static <T> RestBean<T> success(T data) {
        return new RestBean<>(200, true, data, null);
    }

    // 成功响应（带数据和消息）
    public static <T> RestBean<T> success(T data, String msg) {
        return new RestBean<>(200, true, data, msg);
    }

    // 失败响应（仅状态码）
    public static <T> RestBean<T> failure(int code) {
        return new RestBean<>(code, false, null, null);
    }

    // 失败响应（状态码+消息）
    public static <T> RestBean<T> failure(int code, String msg) {
        return new RestBean<>(code, false, null, msg);
    }

    public static RestBean<Map<String, String>> failure(int code, Map<String, String> errors) {
        return new RestBean<>(code, false, errors, "参数校验失败");
    }
}